package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.AddorderBean;
import cst.neu.yuejian_31.iface.AddorderIface;
import cst.neu.yuejian_31.iface.AddorderListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.AddOrderService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/29.
 */

public class AddorderModel implements AddorderIface {
    private Retrofit retrofit;
    public AddorderModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void addOrder(String userId, String classId, String num, String total, final AddorderListener addorderListener) {
        AddOrderService orderService=retrofit.create(AddOrderService.class);
        Call<AddorderBean>call=
                orderService.addOrder(userId,classId,num,total);
        call.enqueue(new Callback<AddorderBean>() {
            @Override
            public void onResponse(Call<AddorderBean> call, Response<AddorderBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    addorderListener.onSuccess(response.body());
                }else {
                    addorderListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<AddorderBean> call, Throwable t) {
                addorderListener.onFail(t.toString());
            }
        });

    }
    @Override
    public void addOrdera(String userId, String activityId, String num, String total,final AddorderListener addorderListenera) {
        AddOrderService orderService = retrofit.create(AddOrderService.class);
        Call<AddorderBean> call =
                orderService.addOrdera(userId, activityId, num, total);
        call.enqueue(new Callback<AddorderBean>() {
            @Override
            public void onResponse(Call<AddorderBean> call, Response<AddorderBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    addorderListenera.onSuccess(response.body());
                } else {
                    addorderListenera.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<AddorderBean> call, Throwable t) {
                addorderListenera.onFail(t.toString());
            }
        });
    }
    @Override
    public void addOrdert (String userId, String trainId, String num, String total,final AddorderListener addorderListenert) {
        AddOrderService orderService = retrofit.create(AddOrderService.class);
        Call<AddorderBean> call =
                orderService.addOrdert (userId, trainId, num, total);
        call.enqueue(new Callback<AddorderBean>() {
            @Override
            public void onResponse(Call<AddorderBean> call, Response<AddorderBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    addorderListenert.onSuccess(response.body());
                } else {
                    addorderListenert.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<AddorderBean> call, Throwable t) {
                addorderListenert.onFail(t.toString());
            }
        });
    }
}
